package com.example.mydy;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.adapter.TaskCompletionAdapter;
import com.example.mydy.model.TaskRecord;
import com.example.mydy.utils.MockDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务完成详情页面 - 展示已完成的任务列表
 */
public class TaskCompletionDetailActivity extends AppCompatActivity {
    private static final String TAG = "TaskCompletionDetail";

    private Button btnBack;
    private TextView tvTaskCount, tvTotalReward;
    private RecyclerView rvCompletedTasks;
    private TaskCompletionAdapter adapter;
    private List<TaskRecord> completedTasks = new ArrayList<>();

    private MockDataManager mockDataManager;
    private String currentUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_completion_detail);

        mockDataManager = MockDataManager.getInstance();
        currentUsername = mockDataManager.getCurrentUser().getUsername();

        initViews();
        setupClickListeners();
        loadTaskData();
    }

    private void initViews() {
        btnBack = findViewById(R.id.btn_back);
        tvTaskCount = findViewById(R.id.tv_task_count);
        tvTotalReward = findViewById(R.id.tv_total_reward);
        rvCompletedTasks = findViewById(R.id.rv_completed_tasks);

        // 设置RecyclerView
        rvCompletedTasks.setLayoutManager(new LinearLayoutManager(this));
        adapter = new TaskCompletionAdapter(this, completedTasks);
        rvCompletedTasks.setAdapter(adapter);
    }

    private void setupClickListeners() {
        btnBack.setOnClickListener(v -> finish());
    }

    private void loadTaskData() {
        // 获取用户完成的任务数据
        List<TaskRecord> userCompletedTasks = mockDataManager.getUserCompletedTasks(currentUsername);
        
        // 获取用户上传任务被完成的记录
        List<TaskRecord> userPostedCompletedTasks = mockDataManager.getUserPostedCompletedTasks(currentUsername);
        
        // 合并两类任务记录
        List<TaskRecord> allTaskRecords = new ArrayList<>();
        allTaskRecords.addAll(userCompletedTasks);
        allTaskRecords.addAll(userPostedCompletedTasks);
        
        // 计算总收益
        double totalReward = 0;
        for (TaskRecord task : allTaskRecords) {
            totalReward += task.getReward();
        }
        
        // 更新UI
        tvTaskCount.setText(String.valueOf(allTaskRecords.size()));
        tvTotalReward.setText(String.format("¥ %.2f", totalReward));
        
        // 更新列表
        completedTasks.clear();
        completedTasks.addAll(allTaskRecords);
        adapter.notifyDataSetChanged();
    }
} 